package com.heiyu.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

public class MoneyUtils {

    public  static String formatter(BigDecimal money,boolean deal){
        if(!deal){
            if(money.doubleValue()<1){
                return money.divide(new BigDecimal("1.000000"),6,BigDecimal.ROUND_HALF_UP)+"";
            }
            return money.divide(new BigDecimal("1.00"),2,BigDecimal.ROUND_HALF_UP)+"";
        }
        if(money == null){
            return "0.000001";
        }
        if(Math.abs(money.doubleValue())>100000000){
            return money.divide(new BigDecimal("100000000.00"), 2, BigDecimal.ROUND_HALF_UP)+"亿";
        }else if(Math.abs(money.doubleValue())>10000){
            return money.divide(new BigDecimal("10000.00"), 2, BigDecimal.ROUND_HALF_UP)+"万";
        }else
        return money.divide(new BigDecimal("1.00"),2,BigDecimal.ROUND_HALF_UP)+"";
    }

    public static BigDecimal changeSymbolType(BigDecimal sourceMoney,BigDecimal symbol){
        if(sourceMoney==null||symbol==null){
            return new BigDecimal("0.00");
        }
       return  sourceMoney.multiply(symbol);
    }

    public static String formatBalance(Long balance){
        if(balance == null){
            return "0.000000";
        }
        if(balance%100000==0)
        return balance/100000+"";
        else{
            return balance/100000.00d+"";
        }
    }

    public static String formatterCoin(BigDecimal money){
        if(money== null){
            return "0.000000";
        }
        if(money.doubleValue()<1)
        return money.divide(new BigDecimal("1.000000"),6,BigDecimal.ROUND_HALF_UP)+"";
        else
            return money.divide(new BigDecimal("1.000000"),2,BigDecimal.ROUND_HALF_UP)+"";
    }



    public static Long wanToFen(String money){
        if(StringUtils.isEmpty(money)){
            return null;
        }
        if(money.indexOf(".")<0){
            money+=".0000";
        }
        long val = Long.valueOf(money.replace(".",""));
        return val;
    }

    public static String fenToWan(Long fen){
        if(fen ==null){
            return "0.00";
        }
        long yushu = fen%1000000;
        long font = (fen-yushu)/1000000;
        return font+"."+yushu+"";
    }

    public static Long yuan2Fen(String yuan){
        if(StringUtils.isEmpty(yuan)){
            return 0l;
        }
        if(yuan.indexOf(".")<0){
            yuan+=".00";
        }
        long val = Long.valueOf(yuan.replace(".",""));
        return val;
    }

    public static Long yuan2Fen(Long yuan){
        if(yuan ==null){
            return 0l;
        }
        return yuan*100l;
    }

    public static String fenToYuan(Integer fen){
        if(fen == null){
            return "0.00";
        }
        String y = fen+"";
        if(y.length()<3){
            y = "00".substring(0,3-y.length())+y;
        }
        y = y.substring(0,y.length()-2)+"."+y.substring(y.length()-2);
        return y+"";
    }
}
